package com.lyw.lib_base.base

import android.app.Activity
import android.app.Application
import android.os.Bundle

open class BaseApplication : Application() {
    private var sDebug: Boolean = false
    private  var sInstance: BaseApplication? = null

    override fun onCreate() {
        super.onCreate()
        setApplication(this)
    }

    private fun setApplication(application: BaseApplication) {
        sInstance = application
        application
            .registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
                override fun onActivityCreated(
                    activity: Activity,
                    savedInstanceState: Bundle?
                ) {
                    AppManager.instance.addActivity(activity)
                }

                override fun onActivityStarted(activity: Activity) {

                }

                override fun onActivityResumed(activity: Activity) {

                }

                override fun onActivityPaused(activity: Activity) {

                }

                override fun onActivityStopped(activity: Activity) {

                }

                override fun onActivitySaveInstanceState(
                    activity: Activity, outState: Bundle
                ) {

                }

                override fun onActivityDestroyed(activity: Activity) {
                    AppManager.instance.removeActivity(activity)
                }
            })
    }
    fun setsDebug(isDebug: Boolean) {
        sDebug = isDebug
    }

    fun issDebug(): Boolean {
        return sDebug
    }
    /**
     * 获得当前app运行的Application
     */
    fun getInstance(): BaseApplication {
        if (sInstance == null) {
            throw NullPointerException(
                "please inherit BaseApplication or call setApplication."
            )
        }
        return sInstance as BaseApplication
    }
}